package ar.edu.unq.wannaplay.employee;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.Interval;

import ar.edu.unq.wannaplay.calendareable.Licence;
import ar.edu.unq.wannaplay.entity.Entity;
import ar.edu.unq.wannaplay.ocupation.Ocupation;

public class Employee extends Entity {

    private static final long serialVersionUID = 1L;

    private Register register;

    private PersonalData data;

    private SocialPlan socialPlan;

    private String art;

    private Ocupation ocupation;

    private List<Skill> skills;

    public Employee() {
        super();
    }

    public Employee(final Register register, final PersonalData data, final SocialPlan socialPlan, final String art,
            final Ocupation ocupation, final List<Skill> skills) {
        super();
        this.register = register;
        this.data = data;
        this.socialPlan = socialPlan;
        this.art = art;
        this.ocupation = ocupation;
        this.skills = skills;
    }

    public void addSkill(final String aKnowledge, final Experience anExperience) {
        final Skill aSkill = new Skill(aKnowledge, anExperience);
        this.getSkills().add(aSkill);
    }

    public void addLicence(final Licence aLicence) {
        this.getRegister().addLicence(aLicence);
    }

    public List<Licence> getLicensesByYear(final int aYear) {
        final List<Licence> licenses = this.getRegister().getLicences();
        final List<Licence> filteredLicenses = new ArrayList<Licence>();

        final DateTime yearInit = new DateTime(aYear, 1, 1, 0, 0, 0, 0);
        final DateTime yearFinish = new DateTime(aYear, 12, 31, 0, 0, 0, 0);

        final Interval yearInterval = new Interval(yearInit, yearFinish);
        for (Licence l : licenses) {
            if (yearInterval.contains(l.getInterval())) {
                filteredLicenses.add(l);
            }
        }

        return filteredLicenses;
    }

    // setters y getters
    public void setSkills(final List<Skill> skills) {
        this.skills = skills;
    }

    public List<Skill> getSkills() {
        return skills;
    }

    public PersonalData getData() {
        return data;
    }

    public void setData(final PersonalData data) {
        this.data = data;
    }

    public Ocupation getOcupation() {
        return ocupation;
    }

    public void setOcupation(final Ocupation ocupation) {
        this.ocupation = ocupation;
    }

    public void setRegister(final Register register) {
        this.register = register;
    }

    public Register getRegister() {
        return register;
    }

    public SocialPlan getSocialPlan() {
        return socialPlan;
    }

    public void setSocialPlan(final SocialPlan socialPlan) {
        this.socialPlan = socialPlan;
    }

    public String getArt() {
        return art;
    }

    public void setArt(final String art) {
        this.art = art;
    }

    public Skill getSkill(final String knowledge) {
        for (Skill skill : this.getSkills()) {
            if (skill.getKnowledge().equals(knowledge)) {
                return skill;
            }
        }
        return null;
    }

    // @Override
    // public String toString() {
    // return data.toString();
    // }

    public boolean hasLicence(final String name, final DateTime dateTime) {
        for (Licence licence : register.getLicences()) {
            if (licence.getName().equals(name)) {
                return licence.getInterval().contains(dateTime);
            }
        }
        return false;
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || !(obj instanceof Employee)) {
            return false;
        }

        Employee other = (Employee) obj;

        if (this.getId() == other.getId()) {
            return true;
        }
        if ((Integer) this.getId() == null) {
            return false;
        }

        // equivalence by id
        return ((Integer) this.getId()).equals(other.getId());
    }

    @Override
    public int hashCode() {
        if ((Integer) this.getId() != null) {
            return ((Integer) this.getId()).hashCode();
        } else {
            return super.hashCode();
        }
    }

}
